const UserOperateModel = require('../models/userOperateModel');
const { validateRequiredFields } = require('../utils/helpers');

class UserOperateController {
  // 收藏视频
  static async collectVideo(req, res) {
    try {
      // 验证必需字段
      validateRequiredFields(req.body, ['openid', 'video_id']);
      
      const { openid, video_id: videoId } = req.body;
      
      const result = await UserOperateModel.collectVideo(openid, videoId);
      
      res.json({
        code: 200,
        msg: result.inserted ? '收藏成功' : '已收藏',
        data: result
      });
    } catch (error) {
      console.error('收藏视频失败:', error);
      res.status(500).json({
        code: 500,
        msg: error.message || '服务器错误'
      });
    }
  }

  // 取消收藏视频
  static async uncollectVideo(req, res) {
    try {
      // 验证必需字段
      validateRequiredFields(req.body, ['openid', 'video_id']);
      
      const { openid, video_id: videoId } = req.body;
      
      await UserOperateModel.uncollectVideo(openid, videoId);
      
      res.json({
        code: 200,
        msg: '取消收藏成功'
      });
    } catch (error) {
      console.error('取消收藏失败:', error);
      res.status(500).json({
        code: 500,
        msg: error.message || '服务器错误'
      });
    }
  }

  // 获取收藏列表
  static async getCollectionList(req, res) {
    try {
      const { openid, page = 1, pageSize = 20 } = req.query;
      
      if (!openid) {
        return res.status(400).json({
          code: 400,
          msg: '缺少用户标识'
        });
      }
      
      const result = await UserOperateModel.getCollectionList(
        openid,
        parseInt(page),
        parseInt(pageSize)
      );
      
      res.json({
        code: 200,
        msg: '获取成功',
        data: result
      });
    } catch (error) {
      console.error('获取收藏列表失败:', error);
      res.status(500).json({
        code: 500,
        msg: error.message || '服务器错误'
      });
    }
  }

  // 记录观看历史
  static async recordHistory(req, res) {
    try {
      // 验证必需字段
      validateRequiredFields(req.body, ['openid', 'video_id']);
      
      const { openid, video_id: videoId, progress = 0 } = req.body;
      
      await UserOperateModel.recordHistory(openid, videoId, progress);
      
      res.json({
        code: 200,
        msg: '历史记录保存成功'
      });
    } catch (error) {
      console.error('记录观看历史失败:', error);
      res.status(500).json({
        code: 500,
        msg: error.message || '服务器错误'
      });
    }
  }

  // 获取观看历史列表
  static async getHistoryList(req, res) {
    try {
      const { openid, page = 1, pageSize = 20 } = req.query;
      
      if (!openid) {
        return res.status(400).json({
          code: 400,
          msg: '缺少用户标识'
        });
      }
      
      const result = await UserOperateModel.getHistoryList(
        openid,
        parseInt(page),
        parseInt(pageSize)
      );
      
      res.json({
        code: 200,
        msg: '获取成功',
        data: result
      });
    } catch (error) {
      console.error('获取观看历史失败:', error);
      res.status(500).json({
        code: 500,
        msg: error.message || '服务器错误'
      });
    }
  }

  // 获取单个视频的观看进度
  static async getVideoProgress(req, res) {
    try {
      const { openid, video_id: videoId } = req.query;
      
      if (!openid || !videoId) {
        return res.status(400).json({
          code: 400,
          msg: '缺少必要参数'
        });
      }
      
      const result = await UserOperateModel.getVideoProgress(openid, videoId);
      
      res.json({
        code: 200,
        msg: '获取成功',
        data: result
      });
    } catch (error) {
      console.error('获取视频进度失败:', error);
      res.status(500).json({
        code: 500,
        msg: error.message || '服务器错误'
      });
    }
  }

  // 检查视频是否已收藏
  static async isCollected(req, res) {
    try {
      const { openid, video_id: videoId } = req.query;
      
      if (!openid || !videoId) {
        return res.status(400).json({
          code: 400,
          msg: '缺少必要参数'
        });
      }
      
      const isCollected = await UserOperateModel.isCollected(openid, videoId);
      
      res.json({
        code: 200,
        msg: '获取成功',
        data: { isCollected }
      });
    } catch (error) {
      console.error('检查收藏状态失败:', error);
      res.status(500).json({
        code: 500,
        msg: error.message || '服务器错误'
      });
    }
  }
}

module.exports = UserOperateController;